<template>
  <div class="td-doctor-me-appointment-time">
    <mt-header fixed title="预约时间设置">
      <router-link to="tdDoctorMe" slot="left">
        <mt-button icon="back"></mt-button>
      </router-link>
      <mt-button slot="right" @click.native="btnEdit();">添加</mt-button>
    </mt-header>

    <mt-cell title="挂职医院" is-link @click.native="showHospital=true">
      <span v-if="!HospitalID">请选择</span>
      <span v-if="HospitalID">{{hospitalList.find(i=>i.value===HospitalID).label}}</span>
    </mt-cell>

    <td-divider v-if="appointmentList.length===0" style="margin-top:100px;">无相关数据</td-divider>

    <div class="list" v-if="appointmentList.length!==0">
      <div class="appointment-item" v-for="(item,index) in appointmentList" :key="index">
        <mt-cell title="编号">
          <span>{{item.AppointmentID}}</span>
        </mt-cell>
        <mt-cell title="预约时间">{{item.AppointmentDate}}</mt-cell>
        <mt-cell title="上午预约/已预约">{{item.AMNumber}}人/{{item.AMHad}}人</mt-cell>
        <mt-cell title="下午预约/已预约">{{item.PMNumber}}人/{{item.PMHad}}人</mt-cell>
        <mt-cell title="晚上预约/已预约">{{item.NMNumber}}人/{{item.NMHad}}人</mt-cell>
        <mt-cell title="操作">
          <span>
            <el-button size="mini" type="warning" @click="btnDelete(item);">删除</el-button>
            <el-button size="mini" type="primary" @click="btnEdit(item);">修改</el-button>
          </span>
        </mt-cell>
      </div>
    </div>
    <!--挂职医院-->
    <mt-popup v-model="showHospital" position="bottom" class="mint-popup-box">
      <mt-header title="挂职医院">
        <mt-button slot="right" @click.native="showHospital=false">关闭</mt-button>
        <mt-button slot="left" @click.native="showHospital=false">确定</mt-button>
      </mt-header>
      <div class="list-box">
        <mt-radio :options="hospitalList" v-model="HospitalID" @change="getHospitalID"></mt-radio>
      </div>
    </mt-popup>
  </div>
</template>

<script>
import DoctorAppointmentService from "../../../../_services/doctorAppointment.service";
export default {
  name: "TianDingDoctorMeAppointmentTimeComponent",
  data() {
    return {
      user: {},
      params: {
        AppointmentID: 0,
        DoctorID: 0,
        HospitalID: 0,
        AppointmentDate: "",
        AMNumber: 0,
        PMNumber: 0,
        NMNumber: 0
      },
      showHospital: false,
      HospitalID: "",
      hospitalList: [],
      appointmentList: []
    };
  },
  mounted() {
    this.user = this.utils$.dataFormat(
      JSON.parse(this.local$.getItem("tdUser"))
    );
    if (this.user.hospitalList.length > 0) {
      this.user.hospitalList.forEach(element => {
        let item = {
          value: element.HospitalID.toString(),
          label: element.HospitalName
        };
        this.HospitalID = item.value;
        this.hospitalList.push(item);
      });
    }
    this.getAppointmentListByDoctorAndHospital(this.HospitalID);
  },
  methods: {
    getAppointmentListByDoctorAndHospital(HospitalID) {
      this.appointmentList = [];
      let params = {};
      params.doctorID = this.user.DoctorID;
      params.hospitalID = parseInt(HospitalID);
      params.pageIndex = 1;
      params.pageSize = 100;
      params.orderby = "CreateTime";
      params.isDesc = true;
      DoctorAppointmentService.getAppointmentList(params).then(response => {
        if (response.Data.Items.length > 0)
          this.appointmentList = response.Data.Items;
      });
    },
    getHospitalID(event) {
      this.showHospital = false;
      this.getAppointmentListByDoctorAndHospital(event);
    },
    btnEdit(item) {
      this.local$.removeItem("tdAppointment");
      if (item !== undefined) {
        this.local$.setItem("tdAppointment", JSON.stringify(item));
      }
      this.$router.push({ name: "tdDoctorMeAppointmentTimeEdit" });
    },
    btnDelete(item) {
      this.confirm$("提示", "确定要删除吗?", {
        confirm: action => {
          this.indicator$.open("请稍后...");
          let params = {};
          params.appointmentID = item.AppointmentID;
          DoctorAppointmentService.deleteAppointmentItem(params).then(
            response => {
              if (response !== null && response.Data > 0) {
                this.toast$("删除成功");
              } else {
                this.toast$("删除失败");
              }
              this.indicator$.close();
              this.getAppointmentItemByDoctorID();
            }
          );
        }
      });
    }
  }
};
</script>

